#ifndef _BUCKET_TCC_
#define _BUCKET_TCC_

#include <algorithm>
#include <mutex>

template<typename Key, typename Value>
typename bucket<Key, Value>::bucket_iterator bucket<Key, Value>::find_entry_for(const Key& key) {
	return find_if(data.begin(), data.end(), [&](const bucket_value& v){return v.first == key;});
}

template<typename Key, typename Value>
Value bucket<Key, Value>::value_for(const Key& key, const Value& defaultValue) const {
	boost::shared_lock<boost::shared_mutex> lock(mutex);
	const bucket_iterator it = find_entry_for(key);
	if (it != data.end()) {
		return it->second;
	}
	return defaultValue;
}

template<typename Key, typename Value>
void bucket<Key, Value>::add_or_update_mapping(const Key& key, const Value& value) {
	std::unique_lock<boost::shared_mutex> lock(mutex);
	const bucket_iterator it = find_entry_for(key);
	if (it != data.end()) {
		it->second = value;
	} else {
		bucket_value newValue = bucket_value(key, value);
		data.push_back(newValue);
	}
}

template<typename Key, typename Value>
void bucket<Key, Value>::remove(const Key& key) {
	std::unique_lock<boost::shared_mutex> lock(mutex);
	const bucket_iterator it = find_entry_for(key);
	if (it != data.end()) {
		data.erase(it);
	}
}

template<typename Key, typename Value>
typename bucket<Key, Value>::bucket_data bucket<Key, Value>::getData() const {
	boost::shared_lock<boost::shared_mutex> lock(mutex);
	return data;
}

#endif
